<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Create_account extends MY_Controller {
	protected $valid_step = array('1', '2', '3', '4', '5', '6', '7', '8', '9', '11', '12', '13', 'review', 'final');
   	function __construct()
	{
		parent::__construct();
		$this->load->helper(array('form', 'url', 'security'));
		$this->load->library(array('form_validation', 'uri', 'session'));
		$this->load->model("users_model", "model", TRUE);
	}

	function index(){
	  $current_step = $this->session->userdata('step');

	   if (!($this->valid_step($current_step) )){
	        $this->session->set_userdata('step', '1');
	        $current_step = $this->session->userdata('step');
//	        redirect(site_url('/create_account/step/' . $current_step), 'location');
	        //$this->step($current_step);
	   }
	   redirect(site_url('/user/create_account/step/' . $current_step), 'location');
//	   else{
//	      $this->step($current_step);
//	   }
	}

 	protected function valid_step($stepID){
	   $current_step = $this->session->userdata('step');
	   if(!in_array($current_step, $this->valid_step)){
	      return false;
	   }
	   if(in_array($stepID, $this->valid_step)){
	      if($stepID == $current_step){
			 return true;
	      }
	      else{
		 redirect(site_url('/user/create_account/step/' . $current_step), 'location');
	      }
	   }
 	   else{
 	      return false;
	   }
 	}

	function step($stepID){
	   if ($this->valid_step($stepID)){
	   	  $data['logged'] = $this->logged;
	      switch($stepID){
			 case '1':
			 	$step1_data = $this->session->userdata('step1_data');
			 	$step1_data['firstName'] = isset($step1_data['firstName']) ? $step1_data['firstName'] : ""; 
			 	$step1_data['logged'] = $this->logged;
			    $this->template->load("user/layout_register", "user/create_step1_view", $step1_data);
			    break;
			    
			 case '2':
			 	$this->session->set_userdata("step", "1");
			    $data['countries']= $this->model->get_countries();
			    $data['logged'] = $this->logged;
			    $this->template->load("user/layout_register", "user/create_step2_view", $data);
			    break;
			    
			 case '3':
			 	//print_r($this->session);
			    $data['countries']= $this->model->get_countries();
			    $step2_data = $this->session->userdata('step2_data');
			    $data['current_countryID'] = $step2_data['countryID'];
			    $data['current_country'] = $step2_data['countryMain'];
			    $data['logged'] = $this->logged;
			    $this->template->load("user/layout_register", "user/create_step3_view", $data);
			    break;
			    
			 case '4':
			    $data['countries']= $this->model->get_countries();
			    $data['available_funds_type'] = $this->model->get_available_ftypes();
			    $data['logged'] = $this->logged;
			    $this->template->load("user/layout_register", "user/create_step4_view", $data);
			    break;
			    
			 case '5':
			    $data['countries']= $this->model->get_countries();
			    $data['available_funds_type'] = $this->model->get_available_ftypes();
			    //$this->session->set_userdata('baseCurrency', 'USD');
			    $step1_data = $this->session->userdata('step1_data');
			    $data['baseCurrency'] = $step1_data['baseCurrency'];
			    $data['frequency'] = $this->model->get_frequencies();
			    $data['time_period'] = $this->model->get_time_period();
			    $data['logged'] = $this->logged;
			    $this->template->load("user/layout_register", "user/create_step5_view", $data);
			    break;
			    
			 case '6':
			 	
			 	//print_r($this->session);
	// 			$this->session->set_userdata('step', '5');
			    $data['countries']= $this->model->get_countries();
			    $data['logged'] = $this->logged;
			    $this->template->load("user/layout_register", "user/create_step6_view", $data);
			    break;
			    
			 case '7':
	//		 	print_r($this->session);
			    $data['countries']= $this->model->get_countries();
			    $step6_data = $this->session->userdata('step6_data');
			    $data['jointCurrent_countryID'] = $step6_data['jointCountryID'];
			    $data['jointCurrent_country'] = $step6_data['jointCountryMain'];
			    $data['logged'] = $this->logged;
			    $this->template->load("user/layout_register", "user/create_step7_view", $data);
			    break;
			    
			  case '8':
			  	//$this->session->set_userdata('step', '6');
			    $data['countries']= $this->model->get_countries();
			    $data['available_funds_type'] = $this->model->get_available_ftypes();
			    $data['logged'] = $this->logged;
			    $this->template->load("user/layout_register", "user/create_step8_view", $data);
			    break;
	
			  case '9':
			    $data['countries']= $this->model->get_countries();
			    $data['available_funds_type'] = $this->model->get_available_ftypes();
			    $data['frequency'] = $this->model->get_frequencies();
			    $data['time_period'] = $this->model->get_time_period();
			    $data['logged'] = $this->logged;
			    $this->template->load("user/layout_register", "user/create_step9_view", $data);
			    break;
	
			  case '11':
			    $data['countries']= $this->model->get_countries();
			    $step1_data = $this->session->userdata("step1_data");
			    $data['_title'] = $step1_data['title'];
			    $data['firstName'] = $step1_data['firstName'];
			    $data['middleName'] = $step1_data['middleName'];
			    $data['lastName'] = $step1_data['lastName'];
			    $data['email'] = $step1_data['email'];
			    $data['logged'] = $this->logged;
			    $this->template->load("user/layout_register", "user/corporate_step1_view", $data);
			    break;
			    
			  case '12':
			  	$data['countries']= $this->model->get_countries();
			    $data['logged'] = $this->logged;
			    $this->template->load("user/layout_register", "user/corporate_step2_view", $data);
			  	break;
			  	
	   		  case '13':
		  	 	$data['countries']= $this->model->get_countries();	
			    $data['available_funds_type'] = $this->model->get_available_ftypes();
			    
			    $step1_data = $this->session->userdata('step1_data');
			    $data['baseCurrency'] = $step1_data['baseCurrency'];
			    $data['frequency'] = $this->model->get_frequencies();
			    $data['time_period'] = $this->model->get_time_period();
			    $data['logged'] = $this->logged;
			    $this->template->load("user/layout_register", "user/corporate_step3_view", $data);
			  	break;
			  	
  			  case 'review' : {
			  	/**
			  	 * 
			  	 * Step 1
			  	 */
			 	$step1_data = $this->session->userdata('step1_data');
			  	$data['_title'] = $step1_data['title'];
			  	$data['firstName'] = $step1_data['firstName'];
			    $data['middleName'] = $step1_data['middleName'];
			    $data['lastName'] = $step1_data['lastName'];
			    $data['email'] = $step1_data['email'];
			    $data['userID'] = $step1_data['userID'];
			    $data['securityQuestion'] = $step1_data['securityQuestion'];
			    $data['accountSize'] = $step1_data['accountSize'];
			    $data['baseCurrency'] = $step1_data['baseCurrency'];
			    $data['accountType'] = $step1_data['accountType'];
			    $data['thirdparty'] = $step1_data['thirdparty'];
			    $data['authorized_trader_input'] = $step1_data['authorized_trader_input'];
			    
			    
			    if($step1_data['accountType'] != 'corporate'){
				    /**
				  	 * 
				  	 * Step 2
				  	 */
				    $step2_data = $this->session->userdata('step2_data');
				    $data['countryID'] = $step2_data['countryID'];
		      		$data['countryMain'] = $step2_data['countryMain'];
					$data['addressLine1Main'] = $step2_data['addressLine1Main'];
				    $data['addressLine2Main'] = $step2_data['addressLine2Main'];
				    $data['cityMain'] = $step2_data['cityMain'];
				    $data['regionMain'] = $step2_data['regionMain'];
				    $data['pCodeMain'] = $step2_data['pCodeMain'];
				    $data['current_years'] = $this->model->get_current_years($step2_data['currentYears']);
				    $data['currentYears'] = $step2_data['currentYears'];
				    $data['homePhoneMain'] = $step2_data['homePhoneMain1'] . "-" . $step2_data['homePhoneMain2'] . "-" . $step2_data['homePhoneMain3'];
				    $data['mobilePhoneMain'] = $step2_data['mobilePhoneMain1'] . "-" . $step2_data['mobilePhoneMain2'] . "-" . $step2_data['mobilePhoneMain3'];
				    
				    if($step2_data['currentYears']  != '4'){
					    $data['countryPrev'] = $this->model->get_country($step2_data['countryPrev']);
					    $data['addressLine1Prev'] = $step2_data['addressLine1Prev'];
					    $data['addressLine2Prev'] = $step2_data['addressLine2Prev'];
					    $data['cityPrev'] = $step2_data['cityPrev'];
					    $data['regionPrev'] = $step2_data['regionPrev'];
					    $data['pCodePrev'] = $step2_data['pCodePrev'];
				    }
				    $data['separateMailing'] = $step2_data['separateMailing'];
				    
				    if ($step2_data['separateMailing']) {
				    	$data['countryMailing'] = $this->model->get_country($step2_data['countryMailing']);
					    $data['addressLine1Mailing'] = $step2_data['addressLine1Mailing'];
					    $data['addressLine2Mailing'] = $step2_data['addressLine2Mailing'];
					    $data['cityMailing'] = $step2_data['cityMailing'];
					    $data['regionMailing'] = $step2_data['regionMailing'];
					    $data['pCodeMailing'] = $step2_data['pCodeMailing'];
				    }
				    
				    /**
				  	 * 
				  	 * Step 3
				  	 */
				    $step3_data = $this->session->userdata('step3_data');
				  	$data['birthDate'] = $step3_data['birthDate'];
				  	$data['citizenship'] = $this->model->get_country($step3_data['citizenship']);
				  	$data['issuingCountry'] = $this->model->get_country($step3_data['issuingCountry']);
				  	$data['passNumber'] = strlen($step3_data['passNumber']) > 0 ? $step3_data['passNumber'] : false;
				  	$data['expiryDate'] = $step3_data['expiryDate'];
				  	
				  	
				  	/**
				  	 * 
				  	 * Step 4
				  	 */
				  	
				  	$step4_data = $this->session->userdata('step4_data');
				  	$data['employmentStatus'] = $step4_data['employmentStatus'];
				  	$data['anualIncome'] = $this->model->get_anual_income($step4_data['anualIncome']);
				  	$data['savingInvest'] = $this->model->get_saving_invest($step4_data['savingInvest']);
				  	$f_sources = explode(";", $step4_data['foundSource']);
				  	$data['foundSource'] = "";
				  	foreach ($f_sources as $item){
					  	$data['foundSource'] .= ($this->model->get_found_source($item) . "/");
				  	}
				  	$data['foundSource'] = trim_slashes($data['foundSource']);
				  	
				  	$data['availableFtype'] = $this->model->get_availability_ftype($step4_data['availableFtype']);
				  	$data['propertyOwner'] = $step4_data['propertyOwner'];
				  	$data['property_owner'] = $this->model->is_property_owner($step4_data['propertyOwner']);
				  	$data['propertyValue'] = $step4_data['propertyValue'];
				  	
				  	if($step4_data['employmentStatus'] == 'Employed'){
					  	$data['employerName'] = $step4_data['employerName'];
					  	$data['employerCountry'] = $this->model->get_country($step4_data['employerCountry']);
					  	$data['addressEmpl1'] = $step4_data['addressEmpl1'];
					  	$data['addressEmpl2'] = $step4_data['addressEmpl2'];
					  	$data['cityEmpl'] = $step4_data['cityEmpl'];
					  	$data['regionEmpl'] = $step4_data['regionEmpl'];
					  	$data['pCodeEmpl'] = $step4_data['pCodeEmpl'];
					  	$data['businessNature'] = $step4_data['businessNature'];
					  	$data['positionEmpl'] = $step4_data['positionEmpl'];
					  	$data['overseasRegulator'] = $step4_data['overseasRegulator'];
				  	}
				  	
				  	/**
				  	 * 
				  	 * Step 5
				  	 */
				  	
				  	$step5_data = $this->session->userdata('step5_data');
				  	$data['initialDeposit'] = $step5_data['initialDeposit'];
				  	$data['sharesFreq'] = $this->model->get_frequency($step5_data['sharesFreq']);
				  	$data['sharesPeriod'] = $this->model->get_tperiod($step5_data['sharesPeriod']);
				  	$data['tradingFreq'] = $this->model->get_frequency($step5_data['tradingFreq']);
				  	$data['tradingPeriod'] = $this->model->get_tperiod($step5_data['tradingPeriod']);
				  	$data['derivativesFreq'] = $this->model->get_frequency($step5_data['derivativesFreq']);
				  	$data['derivativesPeriod'] = $this->model->get_tperiod($step5_data['derivativesPeriod']);
				  	$data['qualified'] = $step5_data['qualified'];
				  	$data['referer'] = $step5_data['referer'];
				  	

	  		  		if(($step1_data['accountType'] == "joint_survivorship" )|| ($step1_data['accountType'] == "joint_common" )){	
					  	/**
					  	 * 
					  	 * Step 6
					  	 */
					  	
					  	$step6_data = $this->session->userdata('step6_data');
					  	$data['jointTitle'] = $step6_data['jointTitle'];
					  	$data['jointFirstName'] = $step6_data['jointFirstName'];
				        $data['jointMiddleName'] = $step6_data['jointMiddleName'];
				    	$data['jointLastName'] = $step6_data['jointLastName'];
					    $data['jointCountryID'] = $step6_data['jointCountryID'];
					    $data['jointCountryMain'] = $step6_data['jointCountryMain'];
					    $data['jointAddressLine1Main'] = $step6_data['jointAddressLine1Main'];
					    $data['jointAddressLine2Main'] = $step6_data['jointAddressLine2Main'];
					    $data['jointCityMain'] = $step6_data['jointCityMain'];
					    $data['jointRegionMain'] = $step6_data['jointRegionMain'];
					    $data['jointPCodeMain'] = $step6_data['jointPCodeMain'];
					    $data['jointCurrentYears'] = $step6_data['jointCurrentYears'];
					    $data['jointCurrent_years'] = $this->model->get_current_years($step6_data['jointCurrentYears']);
					    $data['jointHomePhoneMain'] = $step6_data['jointHomePhoneMain1'] . "-" . $step6_data['jointHomePhoneMain2'] . "-" . $step6_data['jointHomePhoneMain3'];
					    $data['jointMobilePhoneMain'] = $step6_data['jointMobilePhoneMain1']. "-" . $step6_data['jointMobilePhoneMain2'] . "-" . $step6_data['jointMobilePhoneMain3'];
				      	
					    if($step6_data['jointCurrentYears']  != '4'){
						    $data['jointCountryPrev'] =  $this->model->get_country($step6_data['jointCountryPrev']);
					      	$data['jointAddressLine1Prev'] = $step6_data['jointAddressLine1Prev'];
					      	$data['jointAddressLine2Prev'] = $step6_data['jointAddressLine2Prev'];
					      	$data['jointCityPrev'] = $step6_data['jointCityPrev'];
					      	$data['jointRegionPrev'] = $step6_data['jointRegionPrev'];
					      	$data['jointPCodePrev'] = $step6_data['jointPCodePrev'];
					    }
					    
				      	$data['jointSeparateMailing'] = $step6_data['jointSeparateMailing'];
				      	if ($step6_data['jointSeparateMailing']) {
						    $data['jointCountryMailing'] =  $this->model->get_country($step6_data['jointCountryMailing']);
						    $data['jointAddressLine1Mailing'] = $step6_data['jointAddressLine1Mailing'];
					    	$data['jointAddressLine2Mailing'] = $step6_data['jointAddressLine2Mailing'];
					      	$data['jointCityMailing'] = $step6_data['jointCityMailing'];
					      	$data['jointRegionMailing'] = $step6_data['jointRegionMailing'];
					     	$data['jointPCodeMailing'] = $step6_data['jointPCodeMailing'];
				      	}
				     	
				     	/**
					  	 * 
					  	 * Step 7
					  	 */
				     	
					  	$step7_data = $this->session->userdata('step7_data');
				     	$data['jointBirthDate'] = $step7_data['jointBirthDate'];
					  	$data['jointCitizenship'] = $this->model->get_country($step7_data['jointCitizenship']);
					  	$data['jointIssuingCountry'] = $this->model->get_country($step7_data['jointIssuingCountry']);
					  	$data['jointPassNumber'] = strlen($step7_data['jointPassNumber']) > 0 ? $step7_data['jointPassNumber'] : false; 
					  	$data['jointExpiryDate'] = $step7_data['jointExpiryDate'];
				     	
					  	
					  	/**
					  	 * 
					  	 * Step 8
					  	 */
					  	$step8_data = $this->session->userdata('step8_data');
				     	$data['jointEmploymentStatus'] = $step8_data['jointEmploymentStatus'];
					  	$data['jointAnualIncome'] = $this->model->get_anual_income($step8_data['jointAnualIncome']);
					  	$data['jointSavingInvest'] = $this->model->get_saving_invest($step8_data['jointSavingInvest']);
					  	
					  	$f_sources = explode(";", $step8_data['jointFoundSource']);
					  	$data['jointFoundSource'] = "";
					  	foreach ($f_sources as $item){
						  	$data['jointFoundSource'] .= ($this->model->get_found_source($item) . "/");
					  	}
					  	$data['jointFoundSource'] = trim_slashes($data['jointFoundSource']);
					  	
					  	$data['jointAvailableFtype'] = $this->model->get_availability_ftype($step8_data['jointAvailableFtype']);
					  	$data['jointPropertyOwner'] = $step8_data['jointPropertyOwner'];
					  	$data['jointProperty_owner'] = $this->model->is_property_owner($step8_data['jointPropertyOwner']);
					  	$data['jointPropertyValue'] = $step8_data['jointPropertyValue'];
			
					  	if($step8_data['jointEmploymentStatus'] == "Employed"){
						  	$data['jointEmployerName'] = $step8_data['jointEmployerName'];
						  	$data['jointEmployerCountry'] = $this->model->get_country($step8_data['jointEmployerCountry']);
						  	$data['jointAddressEmpl1'] = $step8_data['jointAddressEmpl1'];
						  	$data['jointAddressEmpl2'] = $step8_data['jointAddressEmpl2'];
						  	$data['jointCityEmpl'] = $step8_data['jointCityEmpl'];
						  	$data['jointRegionEmpl'] = $step8_data['jointRegionEmpl'];
						  	$data['jointPCodeEmpl'] = $step8_data['jointPCodeEmpl'];
						  	$data['jointBusinessNature'] = $step8_data['jointBusinessNature'];
						  	$data['jointPositionEmpl'] = $step8_data['jointPositionEmpl'];
						  	$data['jointOverseasRegulator'] = $step8_data['jointOverseasRegulator'];
					  	}
					  	
					  	/**
					  	 * 
					  	 * Step 9
					  	 */
					  	$step9_data = $this->session->userdata('step9_data');
					  	$data['jointSharesFreq'] = $this->model->get_frequency($step9_data['jointSharesFreq']);
					  	$data['jointSharesPeriod'] = $this->model->get_tperiod($step9_data['jointSharesPeriod']);
					  	$data['jointTradingFreq'] = $this->model->get_frequency($step9_data['jointTradingFreq']);
					  	$data['jointTradingPeriod'] = $this->model->get_tperiod($step9_data['jointTradingPeriod']);
					  	$data['jointDerivativesFreq'] = $this->model->get_frequency($step9_data['jointDerivativesFreq']);
					  	$data['jointDerivativesPeriod'] = $this->model->get_tperiod($step9_data['jointDerivativesPeriod']);
					  	$data['jointQualified'] = $step9_data['jointQualified'];
//					  	$data['jointReferer'] = $step9_data['jointReferer'];
	  		  		}
				    
			    }
			    else{
			    	
				  	/**
				  	 * 
				  	 * STEP 11
				  	 */
				  	
				  	$step11_data = $this->session->userdata('step11_data');
				  	$data['companyName']= $step11_data['companyName'];
					$data['regNumber']= $step11_data['regNumber'];
					$data['countryCorpID']= $step11_data['countryCorpID'];
			    	$data['countryCorp']= $step11_data['countryCorp'];
			    	$data['incorpDate']= $step11_data['incorpDate'];	
					$data['countryRegID']= $step11_data['countryRegID'];
					$data['countryReg']= $step11_data['countryReg'];
			    	$data['addressLine1Main']= $step11_data['addressLine1Main'];
					$data['addressLine2Main']= $step11_data['addressLine2Main'];
			    	$data['cityMain']= $step11_data['cityMain'];	
			    	$data['regionMain']= $step11_data['regionMain'];	
				    $data['pCodeMain']= $step11_data['pCodeMain'];
			    	$data['phone']= $step11_data['phone1'] . "-" . $step11_data['phone2'] . "-" . $step11_data['phone3'];
			    	$data['mobilePhone']= $step11_data['mobilePhone1'] . "-" . $step11_data['mobilePhone2'] . "-" . $step11_data['mobilePhone3'];
	
					$data['separateMailing']= $step11_data['separateMailing'];
					$data['countryMailing']= $this->model->get_country($step11_data['countryMailing']);
			    	$data['addressLine1Mailing']= $step11_data['addressLine1Mailing'];
			    	$data['addressLine2Mailing']= $step11_data['addressLine2Mailing'];
			    	$data['cityMailing']= $step11_data['cityMailing'];
			    	$data['regionMailing']= $step11_data['regionMailing'];	
			    	$data['pCodeMailing']= $step11_data['pCodeMailing'];
	//				$data['_title']= $step11_data['_title'];
	//				$data['firstName']= $step11_data['firstName'];
	//				$data['middleName']= $step11_data['middleName'];
	//				$data['lastName']= $step11_data['lastName'];
					$data['position']= $step11_data['position'];
	//				$data['email']= $step11_data['email'];
					$data['offName1']= $step11_data['offName1'];
					$data['offName2']= $step11_data['offName2'];
					$data['offName3']= $step11_data['offName3'];
					$data['offPosition1']= $step11_data['offPosition1'];
					$data['offPosition2']= $step11_data['offPosition2'];
					$data['offPosition3']= $step11_data['offPosition3'];
				  	
					
					/**
					 * STEP 12
					 */
					
					$step12_data = $this->session->userdata("step12_data");
					$data['businessType'] = $step12_data['businessType'];
					$data['regInvestment'] = $step12_data['regInvestment'];
					$data['regulator'] = $step12_data['regulator'];
			    	$data['bRegNumber'] = $step12_data['bRegNumber'];
					$data['anualRevenue'] = $step12_data['anualRevenue'];
				    $data['networth'] = $step12_data['networth'];
				    
				    $data['bankName'] = $step12_data['bankName'];
				    $data['bankCountry'] = $this->model->get_country($step12_data['bankCountry']);
				    $data['addressLine1Bank'] = $step12_data['addressLine1Bank'];
				    $data['addressLine2Bank'] = $step12_data['addressLine2Bank'];
				    $data['cityBank'] = $step12_data['cityBank'];
					$data['regionBank'] = $step12_data['regionBank'];
					$data['pCodeBank'] = $step12_data['pCodeBank'];
				    $data['accName'] = $step12_data['accName'];
				    $data['accNumber'] = $step12_data['accNumber'];
				    $data['sortCode'] = $step12_data['sortCode'];
				    
				    
				    
				    /**
				     * STEP 13
				     */
				    
				    $step13_data = $this->session->userdata("step13_data");
				    $data['initialDeposit'] = $step13_data['initialDeposit'];
					$data['sharesFreq'] = $this->model->get_frequency($step13_data['sharesFreq']);
					$data['sharesPeriod'] = $this->model->get_tperiod($step13_data['sharesPeriod']);
					$data['tradingFreq'] = $this->model->get_frequency($step13_data['tradingFreq']);
					$data['tradingPeriod'] = $this->model->get_tperiod($step13_data['tradingPeriod']);
					$data['derivativesFreq'] = $this->model->get_frequency($step13_data['derivativesFreq']);
					$data['derivativesPeriod'] = $this->model->get_tperiod($step13_data['derivativesPeriod']);
					$data['qualified'] = $step13_data['qualified'];
					$data['referer'] = $step13_data['referer'];
						 
			    }
			    $data['logged'] = $this->logged;
			  	$this->template->load("user/layout_register_review", "user/create_step_review_view", $data);
			    break;
			  }
			  
			  case "final": {
			  	$data['logged'] = $this->logged;
			  	$this->template->load("user/layout_register_review", "user/create_final_view", $data);	
		  		break;
			  }
			 default:
			 	$data['logged'] = $this->logged;
			    $this->template->load("user/layout_register", "user/create_step1_view", $data);
			    break;
		    }
		}
		else{
		   redirect('/user/create_account/', 'location');
		}
      }

	function username_check($str)
		{
			if ($str == 'test')
			{
				$this->form_validation->set_message('username_check', '<li>The %s field can not be the word "test"</li>');
				return FALSE;
			}
			$r = $this->model->check_user_exist($str);
			switch($r){
				case -1 : {
					$this->form_validation->set_message('username_check', '<li>User with this login already exists</li>');
					return FALSE;
				}
				case 1:{
					return TRUE;
				}
			}
		}
		
      function save($stepID){
	 switch($stepID){
	    case '1': {
	       $this->form_validation->set_rules('firstName', 'First Name ', 'required|min_length[2]|max_length[30]');
	       $this->form_validation->set_rules('lastName', 'Last Name', 'required|min_length[2]|max_length[30]');
	       $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|matches[verifyEmail]');
	       $this->form_validation->set_rules('verifyEmail', 'Verify Email', 'required');

	       $this->form_validation->set_rules('userID', 'User ID ', 'trim|required|min_length[5]|max_length[12]|callback_username_check');
//	       $this->form_validation->set_rules('userPasswd', 'Password', 'required|md5');
//	       $this->form_validation->set_rules('userPasswd', 'Password', 'matches[verifyPasswd]');
//	       $this->form_validation->set_rules('verifyPasswd', 'Verify Password', 'trim|required');

	       $this->form_validation->set_rules('securityQuestion', 'Security Question', 'trim|required');
	       $this->form_validation->set_rules('answer', 'Answer', 'trim|required');
	       $this->form_validation->set_rules('answerVerify', 'Verify Answer', 'trim|required');

	       if($this->input->post("thirdparty") == '1'){
			 $this->form_validation->set_rules('authorized_trader_input', 'Authorized Trader', 'trim|required');
	       }

	       if ($this->form_validation->run() == FALSE){
		  $data['error'] = validation_errors();
		  $data['link'] = site_url('user/create_account/step/1#message');
	       }
	       else{
		 if($this->input->post('accountType') == 'joint'){
		    $account_type = $this->input->post('accountTypeJoints');
		 }
		 else{
		   $account_type = $this->input->post('accountType');
		 }
		 
		 $user_data['step1_data'] = array(
		 	'title' => $this->input->post('title'),
		    'firstName' => $this->input->post('firstName'),
		    'middleName' => $this->input->post('middleName'),
		    'lastName' => $this->input->post('lastName'),
		    'email' => $this->input->post('email'),
		    'userID' => $this->input->post('userID'),
		    'securityQuestion' => $this->input->post('securityQuestion'),
		    'answer' => $this->input->post('answer'),
		    'accountSize' => $this->input->post('accountSize'),
		    'baseCurrency' => $this->input->post('baseCurrency'),
		    'accountType' => $account_type,
		    'thirdparty' => $this->input->post('thirdparty'),	
		    'authorized_trader_input' => $this->input->post('authorized_trader_input')
		 );
		 if($account_type == "corporate"){
		 	$this->session->set_userdata('step', '11');
			$this->session->set_userdata($user_data);
			$data['redirect'] = site_url("user/create_account/step/11");
		 }
		 else{
		 	$this->session->set_userdata('step', '2');
			$this->session->set_userdata($user_data);
			$data['redirect'] = site_url("user/create_account/step/2");
		 }
	       }
	       echo json_encode($data);
	       break;
	    }
	    case '2':{
	      $this->form_validation->set_rules('countryMain', 'Country (current)', 'trim|required');
	      $this->form_validation->set_rules('addressLine1Main', 'Address Line 1 (current)', 'trim|required|min_length[4]|max_length[45]');
	      $this->form_validation->set_rules('cityMain', 'City (current)', 'trim|required|min_length[2]|max_length[20]');
	      
	      $this->form_validation->set_rules('currentYears', 'Years of current address', 'trim|required');
	      
	      $this->form_validation->set_rules('homePhoneMain1', 'Home Phone (country code)', 'trim|required|min_length[1]|max_length[6]');
	      $this->form_validation->set_rules('homePhoneMain2', 'Home Phone (area code)', 'trim|required|min_length[1]|max_length[6]');
	      $this->form_validation->set_rules('homePhoneMain3', 'Home Phone (number)', 'trim|required|min_length[5]|max_length[10]');

	      if(!($this->input->post('currentYears') == '' || $this->input->post('currentYears') == '4')){
		$this->form_validation->set_rules('countryPrev', 'Country (Previous)', 'trim|required');
		$this->form_validation->set_rules('addressLine1Prev', 'Address Line 1 (previous)', 'trim|required|min_length[4]|max_length[45]');
		$this->form_validation->set_rules('cityPrev', 'City (previous)', 'trim|required|min_length[2]|max_length[20]');
	      }

	      if($this->input->post('separateMailing') == '1'){
		$this->form_validation->set_rules('countryMailing', 'Country (for mailing)', 'trim|required');
		$this->form_validation->set_rules('addressLine1Mailing', 'Address Line 1 (for mailing)', 'trim|required|min_length[4]|max_length[45]');
		$this->form_validation->set_rules('cityMailing', 'City (for mailing)', 'trim|required|min_length[2]|max_length[20]');
	      }


	      if ($this->form_validation->run() == FALSE){
		$data['error'] = validation_errors();
		$data['link'] = site_url('user/create_account/step/2#message');
	      }
	      else{
		$user_data['step2_data'] = array(
		      'countryID' => $this->input->post('countryMain'),
		      'countryMain' => $this->model->get_country($this->input->post('countryMain')),
		      'addressLine1Main' => $this->input->post('addressLine1Main'),
		      'addressLine2Main' => $this->input->post('addressLine2Main'),
		      'cityMain' => $this->input->post('cityMain'),
		      'regionMain' => $this->input->post('regionMain'),
		      'pCodeMain' => $this->input->post('pCodeMain'),
		      'currentYears' => $this->input->post('currentYears'),
		      'homePhoneMain1' => $this->input->post('homePhoneMain1'),
		      'homePhoneMain2' => $this->input->post('homePhoneMain2'),
		      'homePhoneMain3' => $this->input->post('homePhoneMain3'),
		      'mobilePhoneMain1' => $this->input->post('mobilePhoneMain1'),
		      'mobilePhoneMain2' => $this->input->post('mobilePhoneMain2'),
		      'mobilePhoneMain3' => $this->input->post('mobilePhoneMain3'),

		      'countryPrev' => $this->input->post('countryPrev'),
		      'addressLine1Prev' => $this->input->post('addressLine1Prev'),
		      'addressLine2Prev' => $this->input->post('addressLine2Prev'),
		      'cityPrev' => $this->input->post('cityPrev'),
		      'regionPrev' => $this->input->post('regionPrev'),
		      'pCodePrev' => $this->input->post('pCodePrev'),

		      'separateMailing' => $this->input->post('separateMailing'),
		      
		      'countryMailing' => $this->input->post('countryMailing'),
		      'addressLine1Mailing' => $this->input->post('addressLine1Mailing'),
		      'addressLine2Mailing' => $this->input->post('addressLine2Mailing'),
		      'cityMailing' => $this->input->post('cityMailing'),
		      'regionMailing' => $this->input->post('regionMailing'),
		      'pCodeMailing' => $this->input->post('pCodeMailing')
		      );
		      $this->session->set_userdata('step', '3');

		      $this->session->set_userdata($user_data);

		      $data['redirect'] = site_url("user/create_account/step/3");
	      }
	      echo json_encode($data);
	       break;
	    }
	    case '3':{
	      $this->form_validation->set_rules('birthDay', 'Birth Day', 'trim|required');
	      $this->form_validation->set_rules('birthMonth', 'Birth Month', 'trim|required');
	      $this->form_validation->set_rules('birthYear', 'Birth Year', 'trim|required');
	      
	      $this->form_validation->set_rules('citizenship', 'Citizenship', 'trim|required');
	      
	      $this->form_validation->set_rules('passNumber', 'Passport Number', 'trim|min_length[34]|max_length[40]');
	      	      
	      if ($this->form_validation->run() == FALSE){
			$data['error'] = validation_errors();
			$data['link'] = site_url('user/create_account/step/3#message');
	      }
	      else{
	      	$mk = mktime(0, 0, 0, $this->input->post('birthMonth'), $this->input->post('birthDay'), $this->input->post('birthYear'));
			$birthDate = strftime('%Y-%m-%d',$mk);
	      	if(strlen($this->input->post("passNumber")) > 0){
				
		      	$mk = mktime(0, 0, 0, $this->input->post('expiryMonth'), $this->input->post('expiryDay'), $this->input->post('expiryYear'));
				$expiryDate = strftime('%Y-%m-%d',$mk);
	      	}
	      	else {
	      		$expiryDate = "";
	      	}
			
		$user_data['step3_data'] = array(
		  'birthDate' => $birthDate,
		  'citizenship' => $this->input->post('citizenship'),
		  'issuingCountry' => $this->input->post('issuingCountry'),
		  'passNumber' => $this->input->post('passNumber'),
		  'expiryDate' => $expiryDate
		  );

		  $this->session->set_userdata('step', '4');
		  $this->session->set_userdata($user_data);
		  $data['redirect'] = site_url("user/create_account/step/4");
	      }
	      echo json_encode($data);
	       break;
	    }
	    case '4':{
	      $this->form_validation->set_rules('employmentStatus', 'Employment Status', 'trim|required');

	      if($this->input->post('employmentStatus') == 'Employed'){
			  $this->form_validation->set_rules('employerName', "Employer's Name", 'trim|required|min_length[5]|max_length[20]');
			  $this->form_validation->set_rules('employerCountry', "Country", 'trim|required');
			  $this->form_validation->set_rules('addressLine1Empl', "Address Line 1", 'trim|required|min_length[5]|max_length[40]');
			  $this->form_validation->set_rules('cityEmpl', "City/Town", 'trim|required|min_length[2]|max_length[20]');
			  $this->form_validation->set_rules('businessNature', "Nature of Business", 'trim|required|min_length[4]|max_length[20]');
			  $this->form_validation->set_rules('positionEmpl', "Position held", 'trim|required|min_length[2]|max_length[20]');
			  $this->form_validation->set_rules('overseasRegulator', "Overseas regulator", 'trim|required');
	      }
	      
	      $this->form_validation->set_rules('anualIncome', 'Approx. annual income before tax', 'trim|required');
	      $this->form_validation->set_rules('savingInvest', 'Approx. value of saving and investments', 'trim|required');

	      $this->form_validation->set_rules('foundSource', 'Sources of these funds', 'required');

	      $source = $this->input->post('foundSource');
	      $isOther = false;
	      if($source){
		foreach($source as $item){
		  if($item == '4'){
		      $isOther = true;
		  }
		}
	      }
	      
	      if($isOther){
		  $this->form_validation->set_rules('foundSourceOtherVal', 'Other source', 'trim|required|min_length[5]|max_length[20]');
	      }

	      $this->form_validation->set_rules('availableFtype', 'Approx. amount of funds available for trading with us', 'trim|required');
	      $this->form_validation->set_rules('propertyOwner', 'Do you own property', 'trim|required');

	      if($this->input->post('propertyOwner')) {
			  $this->form_validation->set_rules('propertyValue', 'Approx. value of property (Less mortgage)', 'required|numeric');
	      }
	            

	      if ($this->form_validation->run() == FALSE){
		$data['error'] = validation_errors();
		$data['link'] = site_url('user/create_account/step/4#message');
	      }
	      else{
		$source = $this->input->post('foundSource');
		$foundSource = "";
		if($source){
		  foreach($source as $item){
		    if($item == '4'){
				$foundSource .= $this->input->post('foundSourceOtherVal') . ";";
		    }
		    else{
				$foundSource .= $item . ";";
		    }
		  }
		}
		$propertyValue = 0;
		if($this->input->post('propertyOwner') == '1'){
		    $propertyValue = $this->input->post('propertyValue');
		}
		$user_data['step4_data'] = array(
		  'employmentStatus' => $this->input->post('employmentStatus'),
		  'anualIncome' => $this->input->post('anualIncome'),
		  'savingInvest' => $this->input->post('savingInvest'),
		  'foundSource' => $foundSource,
		  'availableFtype' => $this->input->post('availableFtype'),
		  'propertyOwner' => $this->input->post('propertyOwner'),
		  'propertyValue' => $propertyValue,
		  'employerName' => $this->input->post('employerName'),
		  'employerCountry' => $this->input->post('employerCountry'),
		  'addressEmpl1' => $this->input->post('addressLine1Empl'),
		  'addressEmpl2' => $this->input->post('addressLine2Empl'),
		  'cityEmpl' => $this->input->post('cityEmpl'),
		  'regionEmpl' => $this->input->post('regionEmpl'),
		  'pCodeEmpl' => $this->input->post('pCodeEmpl'),
		  'businessNature' => $this->input->post('businessNature'),
		  'positionEmpl' => $this->input->post('positionEmpl'),
		  'overseasRegulator' => $this->input->post('overseasRegulator')
		  );
		  
		  $this->session->set_userdata('step', '5');
		  $this->session->set_userdata($user_data);
//		  $this->session->set_userdata($empl_data);
		  $data['redirect'] = site_url("user/create_account/step/5");
	      }
	      echo json_encode($data);
	      break;
	    }
	    case '5':{
	      $this->form_validation->set_rules('initialDeposit', 'Initial deposit', 'trim|required|is_natural|max_length[15]');
	      $this->form_validation->set_rules('sharesFreq', 'Shares and/or bons(How frequently?)', 'trim|required');
	      if($this->input->post('sharesFreq') != '1'){
			$this->form_validation->set_rules('sharesPeriod', 'Shares and/or bons(Over what time period?)', 'trim|required');
	      }
	      
	      $this->form_validation->set_rules('tradingFreq', 'Exchange traded derivatives(How frequently?)', 'trim|required');
	      if($this->input->post('tradingFreq') != '1'){
			$this->form_validation->set_rules('tradingPeriod', 'Exchange traded derivatives(Over what time period?)', 'trim|required');
	      }
	      
	      $this->form_validation->set_rules('derivativesFreq', 'OTC derivatives(How frequently?)', 'trim|required');
	      if($this->input->post('derivativesFreq') != '1') {
			$this->form_validation->set_rules('derivativesPeriod', 'OTC derivatives(Over what time period?)', 'trim|required');
	      }
	      
	      $this->form_validation->set_rules('qualified', 'Do you have knowledge or qualifications relevant to our services?', 'trim|required');
	      $this->form_validation->set_rules('referer', 'How did you hear about us?', 'trim|required|min_length[4]|max_length[38]');
	      
	      if ($this->form_validation->run() == FALSE){
			$data['error'] = validation_errors();
			$data['link'] = site_url('user/create_account/step/5#message');
	      }
	      else{
	      		$step1_data = $this->session->userdata('step1_data');
		      $accountType = $step1_data['accountType'];
			  if(($accountType == 'joint_survivorship') || ($accountType == 'joint_common')){
			    $data['redirect'] = site_url("create_account/step/6");
//			    print "step_before:" . $this->session->userdata('step');
			    $this->session->set_userdata('step', '6');
//			    print "step_after:" . $this->session->userdata('step');
			  }
			  else{
			    $data['redirect'] = site_url("user/create_account/step/review");//make redirect to nessesary page(in case that accountType is Joint)
			    $this->session->set_userdata('step', 'review');
			  }
			  
			  $this->input->post('sharesPeriod') ? $sharesPeriod = $this->input->post('sharesPeriod') : $sharesPeriod = 0;
			  $this->input->post('tradingPeriod') ? $tradingPeriod = $this->input->post('tradingPeriod') : $tradingPeriod = 0;
			  $this->input->post('derivativesPeriod') ? $derivativesPeriod = $this->input->post('derivativesPeriod') : $derivativesPeriod = 0;
			  
			  
			$user_data['step5_data'] = array(
			  'initialDeposit' => $this->input->post('initialDeposit'),
			  'sharesFreq' => $this->input->post('sharesFreq'),
			  'sharesPeriod' => $sharesPeriod,
			  'tradingFreq' => $this->input->post('tradingFreq'),
			  'tradingPeriod' => $tradingPeriod,
			  'derivativesFreq' => $this->input->post('derivativesFreq'),
			  'derivativesPeriod' => $derivativesPeriod,
			  'qualified' => $this->input->post('qualified'),
			  'referer' => $this->input->post('referer')
			  );

//			  $this->session->set_userdata('initialDeposit', $this->input->post('initialDeposit'));
//			  $this->session->set_userdata('sharesFreq', $this->input->post('sharesFreq'));
//			  $this->session->set_userdata('sharesPeriod', $sharesPeriod);
//			  $this->session->set_userdata('tradingFreq', $this->input->post('tradingFreq'));
//			  $this->session->set_userdata('tradingPeriod', $tradingPeriod);
//			  $this->session->set_userdata('derivativesFreq', $this->input->post('derivativesFreq'));
//			  $this->session->set_userdata('derivativesPeriod', $derivativesPeriod);
//			  $this->session->set_userdata('qualified', $this->input->post('qualified'));
//			  $this->session->set_userdata('referer', $this->input->post('referer'));
			  //print_r($this->session);
			  $this->session->set_userdata($user_data);
			  
	      }
	      echo json_encode($data);
	       break;
	    }

	    case '6':{
	      $this->form_validation->set_rules('jointFirstName', 'First Name ', 'required|min_length[2]|max_length[30]');
	      $this->form_validation->set_rules('jointLastName', 'Last Name', 'required|min_length[2]|max_length[30]');
	      $this->form_validation->set_rules('jointCountryMain', 'Country (current)', 'trim|required');
	      $this->form_validation->set_rules('jointAddressLine1Main', 'Address Line 1 (current)', 'trim|required|min_length[4]|max_length[45]');
	      $this->form_validation->set_rules('jointCityMain', 'City (current)', 'trim|required|min_length[2]|max_length[20]');

	      $this->form_validation->set_rules('jointCurrentYears', 'Years of current address', 'trim|required');

	      $this->form_validation->set_rules('jointHomePhoneMain1', 'Home Phone (country code)', 'trim|required|min_length[1]|max_length[6]');
	      $this->form_validation->set_rules('jointHomePhoneMain2', 'Home Phone (area code)', 'trim|required|min_length[1]|max_length[6]');
	      $this->form_validation->set_rules('jointHomePhoneMain3', 'Home Phone (number)', 'trim|required|min_length[5]|max_length[10]');

	      if(!($this->input->post('jointCurrentYears') == '' || $this->input->post('jointCurrentYears') == '4')){
			$this->form_validation->set_rules('jointCountryPrev', 'Country (Previous)', 'trim|required');
			$this->form_validation->set_rules('jointAddressLine1Prev', 'Address Line 1 (previous)', 'trim|required|min_length[4]|max_length[45]');
			$this->form_validation->set_rules('jointCityPrev', 'City (previous)', 'trim|required|min_length[2]|max_length[20]');
	      }

	      if($this->input->post('jointSeparateMailing') == '1'){
			$this->form_validation->set_rules('jointCountryMailing', 'Country (for mailing)', 'trim|required');
			$this->form_validation->set_rules('jointAddressLine1Mailing', 'Address Line 1 (for mailing)', 'trim|required|min_length[4]|max_length[45]');
			$this->form_validation->set_rules('jointCityMailing', 'City (for mailing)', 'trim|required|min_length[2]|max_length[20]');
	      }


	      if ($this->form_validation->run() == FALSE){
			$data['error'] = validation_errors();
			$data['link'] = site_url('user/create_account/step/6#message');
	      }
	      else{
		$user_data['step6_data'] = array(
			  'jointTitle' => $this->input->post('jointTitle'),
		      'jointFirstName' => $this->input->post('jointFirstName'),
		      'jointMiddleName' => $this->input->post('jointMiddleName'),
		      'jointLastName' => $this->input->post('jointLastName'),
		      'jointCountryID' => $this->input->post('jointCountryMain'),
		      'jointCountryMain' => $this->model->get_country($this->input->post('jointCountryMain')),
		      'jointAddressLine1Main' => $this->input->post('jointAddressLine1Main'),
		      'jointAddressLine2Main' => $this->input->post('jointAddressLine2Main'),
		      'jointCityMain' => $this->input->post('jointCityMain'),
		      'jointRegionMain' => $this->input->post('jointRegionMain'),
		      'jointPCodeMain' => $this->input->post('jointPCodeMain'),
		      'jointCurrentYears' => $this->input->post('jointCurrentYears'),
		      'jointHomePhoneMain1' => $this->input->post('jointHomePhoneMain1'),
		      'jointHomePhoneMain2' => $this->input->post('jointHomePhoneMain2'),
		      'jointHomePhoneMain3' => $this->input->post('jointHomePhoneMain3'),
		      'jointMobilePhoneMain1' => $this->input->post('jointMobilePhoneMain1'),
		      'jointMobilePhoneMain2' => $this->input->post('jointMobilePhoneMain2'),
		      'jointMobilePhoneMain3' => $this->input->post('jointMobilePhoneMain3'),

		      'jointCountryPrev' => $this->input->post('jointCountryPrev'),
		      'jointAddressLine1Prev' => $this->input->post('jointAddressLine1Prev'),
		      'jointAddressLine2Prev' => $this->input->post('jointAddressLine2Prev'),
		      'jointCityPrev' => $this->input->post('jointCityPrev'),
		      'jointRegionPrev' => $this->input->post('jointRegionPrev'),
		      'jointPCodePrev' => $this->input->post('jointPCodePrev'),

		      'jointSeparateMailing' => $this->input->post('jointSeparateMailing'),

		      'jointCountryMailing' => $this->input->post('jointCountryMailing'),
		      'jointAddressLine1Mailing' => $this->input->post('jointAddressLine1Mailing'),
		      'jointAddressLine2Mailing' => $this->input->post('jointAddressLine2Mailing'),
		      'jointCityMailing' => $this->input->post('jointCityMailing'),
		      'jointRegionMailing' => $this->input->post('jointRegionMailing'),
		      'jointPCodeMailing' => $this->input->post('jointPCodeMailing')
		      );
		      
		      $this->session->set_userdata($user_data);
		      
		      $this->session->set_userdata('step', '7');
		      $data['redirect'] = site_url("user/create_account/step/7");
		      
		      
		      
	      }
	      //print_r($this->session);
	      echo json_encode($data);
	       break;
	    }
 
	    case '7':{
	      $this->form_validation->set_rules('jointBirthDay', 'Birth Day', 'trim|required');
	      $this->form_validation->set_rules('jointBirthMonth', 'Birth Month', 'trim|required');
	      $this->form_validation->set_rules('jointBirthYear', 'Birth Year', 'trim|required');

	      $this->form_validation->set_rules('jointCitizenship', 'Citizenship', 'trim|required');

	      $this->form_validation->set_rules('jointPassNumber', 'Passport Number', 'trim|min_length[34]|max_length[40]');
	
	      if ($this->form_validation->run() == FALSE){
			$data['error'] = validation_errors();
			$data['link'] = site_url('user/create_account/step/7#message');
	      }
	      else{
	      	
	      	$mk = mktime(0, 0, 0, $this->input->post('jointBirthMonth'), $this->input->post('jointBirthDay'), $this->input->post('jointBirthYear'));
			$birthDate = strftime('%Y-%m-%d',$mk);
			
			if(strlen($this->input->post("jointPassNumber")) > 0){
		      	$mk = mktime(0, 0, 0, $this->input->post('jointExpiryMonth'), $this->input->post('jointExpiryDay'), $this->input->post('jointExpiryYear'));
				$expiryDate = strftime('%Y-%m-%d',$mk);
			}
			else{
				$expiryDate = "";
			}
			
	      	$expiryDate = $expiryDate == "1970-01-01" ? "" : $expiryDate;
		$user_data['step7_data'] = array(
		  'jointBirthDate' => $birthDate,
		  'jointCitizenship' => $this->input->post('jointCitizenship'),
		  'jointIssuingCountry' => $this->input->post('jointIssuingCountry'),
		  'jointPassNumber' => $this->input->post('jointPassNumber'),
		  'jointExpiryDate' => $expiryDate
		  );
		  $this->session->set_userdata('step', '8');
		  $this->session->set_userdata($user_data);
		  $data['redirect'] = site_url("user/create_account/step/8");
	      }
	      
	      echo json_encode($data);
	       break;
	    }

	    case '8':{
	      $this->form_validation->set_rules('jointEmploymentStatus', 'Employment Status', 'trim|required');

	      if($this->input->post('jointEmploymentStatus') == 'Employed'){
		  $this->form_validation->set_rules('jointEmployerName', "Employer's Name", 'trim|required|min_length[5]|max_length[20]');
		  $this->form_validation->set_rules('jointEmployerCountry', "Country", 'trim|required');
		  $this->form_validation->set_rules('jointAddressLine1Empl', "Address Line 1", 'trim|required|min_length[5]|max_length[40]');
		  $this->form_validation->set_rules('jointCityEmpl', "City/Town", 'trim|required|min_length[2]|max_length[20]');
		  $this->form_validation->set_rules('jointBusinessNature', "Nature of Business", 'trim|required|min_length[4]|max_length[20]');
		  $this->form_validation->set_rules('jointPositionEmpl', "Position held", 'trim|required|min_length[2]|max_length[20]');
		  $this->form_validation->set_rules('jointOverseasRegulator', "Overseas regulator", 'trim|required');
	      }

	      $this->form_validation->set_rules('jointAnualIncome', 'Approx. annual income before tax', 'trim|required');
	      $this->form_validation->set_rules('jointSavingInvest', 'Approx. value of saving and investments', 'trim|required');

	      $this->form_validation->set_rules('jointFoundSource', 'Sources of these funds', 'required');

	      $source = $this->input->post('jointFoundSource');
	      $isOther = false;
	      if($source){
		foreach($source as $item){
		  if($item == '4'){
		      $isOther = true;
		  }
		}
	      }

	      if($isOther){
		  $this->form_validation->set_rules('jointFoundSourceOtherVal', 'Other source', 'trim|required|min_length[5]|max_length[20]');
	      }

	      $this->form_validation->set_rules('jointAvailableFtype', 'Approx. amount of funds available for trading with us', 'trim|required');
	      $this->form_validation->set_rules('jointPropertyOwner', 'Do you own property', 'trim|required');

	      if($this->input->post('jointPropertyOwner') == '1'){
		  $this->form_validation->set_rules('jointPropertyValue', 'Approx. value of property (Less mortgage)', 'trim|required|number');
	      }


	      if ($this->form_validation->run() == FALSE){
		$data['error'] = validation_errors();
		$data['link'] = site_url('user/create_account/step/8#message');
	      }
	      else{
		$source = $this->input->post('jointFoundSource');
		$foundSource = "";
		if($source){
		  foreach($source as $item){
		    if($item == '4'){
			$foundSource .= $this->input->post('jointFoundSourceOtherVal') . ";";
		    }
		    else{
			$foundSource .= $item . ";";
		    }
		  }
		}
		$propertyValue = 0;
		if($this->input->post('jointPropertyOwner') == '1'){
		    $propertyValue = $this->input->post('jointPropertyValue');
		}
		$user_data['step8_data'] = array(
		  'jointEmploymentStatus' => $this->input->post('jointEmploymentStatus'),
		  'jointAnualIncome' => $this->input->post('jointAnualIncome'),
		  'jointSavingInvest' => $this->input->post('jointSavingInvest'),
		  'jointFoundSource' => $foundSource,
		  'jointAvailableFtype' => $this->input->post('jointAvailableFtype'),
		  'jointPropertyOwner' => $this->input->post('jointPropertyOwner'),
		  'jointPropertyValue' => $propertyValue,
		  'jointEmployerName' => $this->input->post('jointEmployerName'),
		  'jointEmployerCountry' => $this->input->post('jointEmployerCountry'),
		  'jointAddressEmpl1' => $this->input->post('jointAddressLine1Empl'),
		  'jointAddressEmpl2' => $this->input->post('jointAddressLine2Empl'),
		  'jointCityEmpl' => $this->input->post('jointCityEmpl'),
		  'jointRegionEmpl' => $this->input->post('jointRegionEmpl'),
		  'jointPCodeEmpl' => $this->input->post('jointPCodeEmpl'),
		  'jointBusinessNature' => $this->input->post('jointBusinessNature'),
		  'jointPositionEmpl' => $this->input->post('jointPositionEmpl'),
		  'jointOverseasRegulator' => $this->input->post('jointOverseasRegulator')
		  );
		  $this->session->set_userdata('step', '9');
		  $this->session->set_userdata($user_data);
//		  $this->session->set_userdata($empl_data);
		  $data['redirect'] = site_url("user/create_account/step/9");
	      }
	      echo json_encode($data);
	      break;
	    }

	    case '9':{
	     	 $this->form_validation->set_rules('jointSharesFreq', 'Shares and/or bons(How frequently?)', 'trim|required');
	    	 if($this->input->post('jointSharesFreq') != '1'){
				 $this->form_validation->set_rules('jointSharesPeriod', 'Shares and/or bons(Over what time period?)', 'trim|required');
	      }

	      $this->form_validation->set_rules('jointTradingFreq', 'Exchange traded derivatives(How frequently?)', 'trim|required');
	      if($this->input->post('jointTradingFreq') != '1'){
	      	$this->form_validation->set_rules('jointTradingPeriod', 'Exchange traded derivatives(Over what time period?)', 'trim|required');
	      }

	      $this->form_validation->set_rules('jointDerivativesFreq', 'OTC derivatives(How frequently?)', 'trim|required');
	      if($this->input->post('jointDerivativesFreq') != '1') {
			$this->form_validation->set_rules('jointDerivativesPeriod', 'OTC derivatives(Over what time period?)', 'trim|required');
	      }

	      $this->form_validation->set_rules('jointQualified', 'Do you have knowledge or qualifications relevant to our services?', 'trim|required');
//	      $this->form_validation->set_rules('jointReferer', 'How did you hear about us?', 'trim|required|min_length[4]|max_length[38]');

	      if ($this->form_validation->run() == FALSE){
			$data['error'] = validation_errors();
			$data['link'] = site_url('user/create_account/step/9#message');
	      }
	      else{
			$user_data['step9_data'] = array(
		  'jointInitialDeposit' => $this->input->post('jointInitialDeposit'),
		  'jointSharesFreq' => $this->input->post('jointSharesFreq'),
		  'jointSharesPeriod' => $this->input->post('jointSharesPeriod'),
		  'jointTradingFreq' => $this->input->post('jointTradingFreq'),
		  'jointTradingPeriod' => $this->input->post('jointTradingPeriod'),
		  'jointDerivativesFreq' => $this->input->post('jointDerivativesFreq'),
		  'jointDerivativesPeriod' => $this->input->post('jointDerivativesPeriod'),
		  'jointQualified' => $this->input->post('jointQualified'),
		  
		  );
//'jointReferer' => $this->input->post('jointReferer')
		  $this->session->set_userdata($user_data);
  
		  $data['redirect'] = site_url("user/create_account/step/review");
		  $this->session->set_userdata('step', 'review');
	      }
	      echo json_encode($data);
	       break;
	    }
	    
	 	case '11':{
	 	  $this->form_validation->set_rules('companyName', 'Company Name', 'trim|required|min_length[5]|max_length[35]');
	 	  $this->form_validation->set_rules('regNumber', 'Company Registered Number', 'trim|required|min_length[10]|max_length[40]');
	      $this->form_validation->set_rules('countryCorp', 'Country of Incorporation', 'trim|required');
	      
	      $this->form_validation->set_rules('incorpDay', 'Day of Incorporation', 'trim|required');
	      $this->form_validation->set_rules('incorpMonth', 'Month of Incorporation', 'trim|required');
	      $this->form_validation->set_rules('incorpYear', 'Year of Incorporation', 'trim|required');
	      
	      $this->form_validation->set_rules('countryReg', 'Registration Country', 'trim|required');
	      $this->form_validation->set_rules('addressLine1Main', 'Address Line 1 (registration)', 'trim|required|min_length[4]|max_length[45]');
	      $this->form_validation->set_rules('cityMain', 'City (registration)', 'trim|required|min_length[2]|max_length[20]');
	      
	      $this->form_validation->set_rules('phone1', 'Phone (country code)', 'trim|required|min_length[1]|max_length[6]');
	      $this->form_validation->set_rules('phone2', 'Phone (area code)', 'trim|required|min_length[1]|max_length[6]');
	      $this->form_validation->set_rules('phone3', 'Phone (number)', 'trim|required|min_length[5]|max_length[10]');

	      if($this->input->post('separateMailing') == '1'){
			$this->form_validation->set_rules('countryMailing', 'Country (for mailing)', 'trim|required');
			$this->form_validation->set_rules('addressLine1Mailing', 'Address Line 1 (for mailing)', 'trim|required|min_length[4]|max_length[45]');
			$this->form_validation->set_rules('cityMailing', 'City (for mailing)', 'trim|required|min_length[2]|max_length[20]');
	      }
	      
	      $this->form_validation->set_rules('title', 'Title (Principal Contact)', 'trim|required');
	      $this->form_validation->set_rules('firstName', 'First Name (Principal Contact)', 'trim|required|min_length[2]|max_length[25]');
	      $this->form_validation->set_rules('lastName', 'Last Name (Principal Contact)', 'trim|required|min_length[2]|max_length[25]');
	      $this->form_validation->set_rules('position', 'Position (Principal Contact)', 'trim|required|min_length[4]|max_length[30]');
	      
	      $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|matches[verifyEmail]');
	      $this->form_validation->set_rules('verifyEmail', 'Verify Email', 'required');
	      
	      
	      $this->form_validation->set_rules('offName1', 'Name (Other officer)', 'trim|required|min_length[5]|max_length[40]');
	      $this->form_validation->set_rules('offPosition1', 'Position (Other officer)', 'trim|required|min_length[5]|max_length[30]');
	      

	      if ($this->form_validation->run() == FALSE){
			$data['error'] = validation_errors();
			$data['link'] = site_url('user/create_account/step/11#message');
	      }
	      else{
	      	$mk = mktime(0, 0, 0, $this->input->post('incorpMonth'), $this->input->post('incorpDay'), $this->input->post('incorpYear'));
			$incorpDate = strftime('%Y-%m-%d',$mk);
			
			$user_data['step11_data'] = array(
		    'companyName' => $this->input->post('companyName'),
			'regNumber' => $this->input->post('regNumber'),
			'countryCorpID' => $this->input->post('countryCorp'),
		    'countryCorp' => $this->model->get_country($this->input->post('countryCorp')),
		    'incorpDate' => $incorpDate,
			'countryRegID' => $this->input->post('countryReg'),
			'countryReg' => $this->model->get_country($this->input->post('countryReg')),
		    'addressLine1Main' => $this->input->post('addressLine1Main'),
			'addressLine2Main' => $this->input->post('addressLine2Main'),
		    'cityMain' => $this->input->post('cityMain'),
		    'regionMain' => $this->input->post('regionMain'),
		    'pCodeMain' => $this->input->post('pCodeMain'),
		    'phone1' => $this->input->post('phone1'),
		    'phone2' => $this->input->post('phone2'),
		    'phone3' => $this->input->post('phone3'),
		    'mobilePhone1' => $this->input->post('mobilePhone1'),
		    'mobilePhone2' => $this->input->post('mobilePhone2'),
		    'mobilePhone3' => $this->input->post('mobilePhone3'),

			'separateMailing' => $this->input->post('separateMailing'),
			'countryMailing' => $this->input->post('countryMailing'),
		    'addressLine1Mailing' => $this->input->post('addressLine1Mailing'),
		    'addressLine2Mailing' => $this->input->post('addressLine2Mailing'),
		    'cityMailing' => $this->input->post('cityMailing'),
		    'regionMailing' => $this->input->post('regionMailing'),
		    'pCodeMailing' => $this->input->post('pCodeMailing'),
			'position' => $this->input->post('position'),
			'offName1' => $this->input->post('offName1'),
			'offName2' => $this->input->post('offName2'),
			'offName3' => $this->input->post('offName3'),
			'offPosition1' => $this->input->post('offPosition1'),
			'offPosition2' => $this->input->post('offPosition2'),
			'offPosition3' => $this->input->post('offPosition3')
		      );
		      
		      
		      $step1_data = $this->session->userdata("step1_data");
		      
		      $user_data['step1_data'] = array(
		 	'title' => $this->input->post('title'),
		    'firstName' => $this->input->post('firstName'),
		    'middleName' => $this->input->post('middleName'),
		    'lastName' => $this->input->post('lastName'),
		    'email' => $this->input->post('email'),
		    'userID' => $step1_data['userID'],
		    'securityQuestion' => $step1_data['securityQuestion'],
		    'answer' => $step1_data['answer'],
		    'accountSize' => $step1_data['accountSize'],
		    'baseCurrency' => $step1_data['baseCurrency'],
		    'accountType' => $step1_data['accountType'],
		    'thirdparty' => $step1_data['thirdparty'],	
		    'authorized_trader_input' => $step1_data['authorized_trader_input']
		 	);
		 	
		      $this->session->set_userdata('step', '12');
			  $this->session->set_userdata($user_data);

		      $data['redirect'] = site_url("user/create_account/step/12");
	      }
	      echo json_encode($data);
	       break;
	    }
	    
	 	case "12": {
	 		unset($data);
	 	  $this->form_validation->set_rules('businessType', 'Type of Business', 'trim|required|min_length[5]|max_length[25]');
	 	  $this->form_validation->set_rules('regInvestment', 'Is the company registered to conduct investment business?', 'trim|required');
	      if($this->input->post("regInvestment") == "1"){
		 	  $this->form_validation->set_rules('regulator', 'Regulator', 'trim|required|min_length[4]|max_length[25]');
		 	  $this->form_validation->set_rules('bRegNumber', 'Registration Number', 'trim|required|min_length[10]|max_length[25]');
	      }
	      
	      $this->form_validation->set_rules('anualRevenue', 'Annual turnover of the company', 'trim|required|max_length[15]|numeric');
	      $this->form_validation->set_rules('networth', 'Company balance sheet/networth', 'trim|required|max_length[15]|numeric');

	      
	      $this->form_validation->set_rules('bankName', 'Bank Name', 'trim|required|min_length[4]|max_length[25]');
	      $this->form_validation->set_rules('bankCountry', 'Bank Country', 'trim|required');
	      $this->form_validation->set_rules('addressLine1Bank', 'Address line 1', 'trim|required|min_length[5]|max_length[45]');
	      $this->form_validation->set_rules('cityBank', 'City', 'trim|required|min_length[2]|max_length[25]');
	      
	      $this->form_validation->set_rules('accName', 'Account Name', 'trim|required|min_length[5]|max_length[20]');
	      $this->form_validation->set_rules('accNumber', 'Account Number', 'trim|required|min_length[8]|max_length[25]');
	      $this->form_validation->set_rules('sortCode', 'Sort Code/BIC', 'trim|required|min_length[2]|max_length[25]');
	      
	      if ($this->form_validation->run() == FALSE){
			$data['error'] = validation_errors();
			$data['link'] = site_url('user/create_account/step/12#message');
//			print_r($data);
	      }
	      else{
			$user_data['step12_data'] = array(
		    'businessType' => $this->input->post('businessType'),
			'regInvestment' => $this->input->post('regInvestment'),
			'regulator' => $this->input->post('regulator'),
		    'bRegNumber' => $this->input->post('bRegNumber'),
			'anualRevenue' => $this->input->post('anualRevenue'),
		    'networth' => $this->input->post('networth'),
		    
		    'bankName' => $this->input->post('bankName'),
		    'bankCountry' => $this->input->post('bankCountry'),
		    'addressLine1Bank' => $this->input->post('addressLine1Bank'),
		    'addressLine2Bank' => $this->input->post('addressLine2Bank'),
		    'cityBank' => $this->input->post('cityBank'),
			'regionBank' => $this->input->post('regionBank'),
			'pCodeBank' => $this->input->post('pCodeBank'),
		    'accName' => $this->input->post('accName'),
		    'accNumber' => $this->input->post('accNumber'),
		    'sortCode' => $this->input->post('sortCode')
		      );
		      $this->session->set_userdata('step', '13');

		      $this->session->set_userdata($user_data);

		      $data['redirect'] = site_url("user/create_account/step/13");
		      
	 		}
	       	echo json_encode($data);
	 		break;
	 	}
	 	
		case "13": {
		  $this->form_validation->set_rules('initialDeposit', 'Initial deposit', 'trim|required|is_natural|max_length[15]');
	      $this->form_validation->set_rules('sharesFreq', 'Shares and/or bons(How frequently?)', 'trim|required');
	      if($this->input->post('sharesFreq') != '1'){
			$this->form_validation->set_rules('sharesPeriod', 'Shares and/or bons(Over what time period?)', 'trim|required');
	      }
	      
	      $this->form_validation->set_rules('tradingFreq', 'Exchange traded derivatives(How frequently?)', 'trim|required');
	      if($this->input->post('tradingFreq') != '1'){
			$this->form_validation->set_rules('tradingPeriod', 'Exchange traded derivatives(Over what time period?)', 'trim|required');
	      }
	      
	      $this->form_validation->set_rules('derivativesFreq', 'OTC derivatives(How frequently?)', 'trim|required');
	      if($this->input->post('derivativesFreq') != '1') {
			$this->form_validation->set_rules('derivativesPeriod', 'OTC derivatives(Over what time period?)', 'trim|required');
	      }
	      
	      $this->form_validation->set_rules('qualified', 'Do you have knowledge or qualifications relevant to our services?', 'trim|required');
	      $this->form_validation->set_rules('referer', 'How did you hear about us?', 'trim|required|min_length[4]|max_length[38]');
	      
	      if ($this->form_validation->run() == FALSE){
			$data['error'] = validation_errors();
			$data['link'] = site_url('user/create_account/step/13#message');
	      }
	      else{
			  $this->input->post('sharesPeriod') ? $sharesPeriod = $this->input->post('sharesPeriod') : $sharesPeriod = 0;
			  $this->input->post('tradingPeriod') ? $tradingPeriod = $this->input->post('tradingPeriod') : $tradingPeriod = 0;
			  $this->input->post('derivativesPeriod') ? $derivativesPeriod = $this->input->post('derivativesPeriod') : $derivativesPeriod = 0;
			  
			  
			$user_data['step13_data'] = array(
			  'initialDeposit' => $this->input->post('initialDeposit'),
			  'sharesFreq' => $this->input->post('sharesFreq'),
			  'sharesPeriod' => $sharesPeriod,
			  'tradingFreq' => $this->input->post('tradingFreq'),
			  'tradingPeriod' => $tradingPeriod,
			  'derivativesFreq' => $this->input->post('derivativesFreq'),
			  'derivativesPeriod' => $derivativesPeriod,
			  'qualified' => $this->input->post('qualified'),
			  'referer' => $this->input->post('referer')
			  );

			  $this->session->set_userdata($user_data);
			  $data['redirect'] = site_url("user/create_account/step/review");
		      $this->session->set_userdata('step', 'review');
	      }
	      echo json_encode($data);
	       break;
	 	}
	    
	    case "review": {
	    	$data['redirect'] = site_url("user/create_account/step/final");
		 	$this->session->set_userdata('step', 'final');
		 	 echo json_encode($data);
	    	break;
	    }
	    
	    case "final": {
	    	// TODO add support for user database saving
	    	$user = new User();
	    	/*
	    	 * STEP 1 DATA
	    	 */
	    	$step1_data = $this->session->userdata('step1_data');
	    	$user->TITLE = $step1_data['title'];
		  	$user->FIRST_NAME = $step1_data['firstName'];
		    $user->MIDDLE_NAME = $step1_data['middleName'];
		    $user->LAST_NAME = $step1_data['lastName'];
		    $user->MAIL_ADDRESS = $step1_data['email'];
		    $user->USER_NAME = $step1_data['userID'];
		    // TODO generate auto password for each user
		    $userPasswd = random_string('alnum', 8);
		    $user->USER_PASSWD = do_hash($userPasswd);
		    $user->SEC_Q = $step1_data['securityQuestion'];
		    $user->SEC_Q_ANSWER = $step1_data['answer'];
		    $user->ACCOUNT_PREFERENCES_ID = $step1_data['accountSize'];
		    $user->BASE_CURRENCY = $step1_data['baseCurrency'];
		    $user->ACCOUNT_TYPE_ID = $user->get_account_type_ID($step1_data['accountType']);
		    $user->MANAGED_AUTHORIZED = $step1_data['authorized_trader_input'];
		    
		    
		    if($step1_data['accountType'] != 'corporate'){
			    /*
		    	 * STEP 2 DATA
		    	 */
		    	$step2_data = $this->session->userdata('step2_data');
			    $user->COUNTRY_CODE = $step2_data['countryID'];
				$user->ADDRESS_MAIN = $step2_data['addressLine1Main'];
			    $user->ADDRESS_SECOND = $step2_data['addressLine2Main'];
			    $user->CITY = $step2_data['cityMain'];
			    $user->STATE = $step2_data['regionMain'];
			    $user->POSTAL_CODE = $step2_data['pCodeMain'];
			    $user->CURRENT_ADDR_YEARS = $step2_data['currentYears'];
			    $user->HOME_PHONE = $step2_data['homePhoneMain1'] . "-" . $step2_data['homePhoneMain2'] . "-" . $step2_data['homePhoneMain3'];
			    $user->MOBILE_PHONE = $step2_data['mobilePhoneMain1'] . "-" . $step2_data['mobilePhoneMain2'] . "-" . $step2_data['mobilePhoneMain3'];
			    
			    $prevAddr = new PrevAddress();
			    if($step2_data['currentYears']  != '4'){
				    $prevAddr->COUNTRY_CODE = $step2_data['countryPrev'];
				    $prevAddr->ADDRESS_MAIN = $step2_data['addressLine1Prev'];
				    $prevAddr->ADDRESS_SECOND = $step2_data['addressLine2Prev'];
				    $prevAddr->CITY = $step2_data['cityPrev'];
				    $prevAddr->STATE = $step2_data['regionPrev'];
				    $prevAddr->POSTAL_CODE = $step2_data['pCodePrev'];
			    }
			    
			    $user->SEPARATE_MAILING = $step2_data['separateMailing'];
			    
			    $mailing = new MailingAddress();
			    if ($step2_data['separateMailing']) {
			    	$mailing->COUNTRY_CODE = $step2_data['countryMailing'];
				    $mailing->ADDRESS_MAIN = $step2_data['addressLine1Mailing'];
				    $mailing->ADDRESS_SECOND = $step2_data['addressLine2Mailing'];
				    $mailing->CITY = $step2_data['cityMailing'];
				    $mailing->STATE = $step2_data['regionMailing'];
				    $mailing->POSTAL_CODE = $step2_data['pCodeMailing'];
			    }
			    
			    
			    /*
			     * STEP 3
			     */
			    $step3_data = $this->session->userdata('step3_data');
			  	$user->BIRTHDATE = $step3_data['birthDate'];
			  	$user->CITIZENSHIP_COUNTRY = $step3_data['citizenship'];
			  	$user->ISSUING_COUNTRY = $this->model->get_country($step3_data['issuingCountry']);
			  	$user->PASS_NUMBER = strlen($step3_data['passNumber']) > 0 ? $step3_data['passNumber'] : "";
			  	$user->EXPIRY_DATE = $step3_data['expiryDate'];
			    
			    /*
			     * STEP 4
			     */
			  	$step4_data = $this->session->userdata('step4_data');
			  	$user->EMPLOYMENT_STATUS = $step4_data['employmentStatus'];
			  	$user->INCOME_TYPE_ID = $step4_data['anualIncome'];
			  	$user->SAVINGS_TYPE_ID = $step4_data['savingInvest'];
			  	$f_sources = explode(";", $step4_data['foundSource']);
			  	$user->FUND_SOURCE = "";
			  	foreach ($f_sources as $item){
				  	$user->FUND_SOURCE .= ($this->model->get_found_source($item) . "/");
			  	}
			  	$user->FUND_SOURCE = trim_slashes($user->FUND_SOURCE);
			  	
			  	$user->FUNDS_AV_TYPE = $step4_data['availableFtype'];
			  	$user->PROPERTY_OWNER = $step4_data['propertyOwner'];
			  	$user->PROPERTY_VALUE = $step4_data['propertyValue'];
			  	
			  	$employer = new Employer();
			  	if($step4_data['employmentStatus'] == 'Employed'){
				  	$employer->E_NAME = $step4_data['employerName'];
				  	$employer->COUNTRY_ID = $step4_data['employerCountry'];
				  	$employer->ADDRESS_MAIN = $step4_data['addressEmpl1'];
				  	$employer->ADDRESS_SECOND = $step4_data['addressEmpl2'];
				  	$employer->CITY = $step4_data['cityEmpl'];
				  	$employer->STATE = $step4_data['regionEmpl'];
				  	$employer->POSTAL_CODE = $step4_data['pCodeEmpl'];
				  	$employer->BUSINESS_NATURE = $step4_data['businessNature'];
				  	$employer->E_POSITION = $step4_data['positionEmpl'];
				  	$employer->OVERSEAS_REGULATOR = $step4_data['overseasRegulator'];
			  	}
			  	
			  	/**
			  	 * 
			  	 * STEP 5
			  	 */
			  	
			  	$step5_data = $this->session->userdata('step5_data');
			  	$user->INITIAL_DEPOSIT = $step5_data['initialDeposit'];
			  	$user->SHARES_BONS_FREQUENCY = $step5_data['sharesFreq'];
			  	$user->SHARES_BONS_PERIOD = $step5_data['sharesPeriod'];
			  	$user->DERIVATIVES_FREQUENCY = $step5_data['tradingFreq'];
			  	$user->DERIVATIVES_PERIOD = $step5_data['tradingPeriod'];
			  	$user->OTC_DERIVATIVES_FREQUENCY = $step5_data['derivativesFreq'];
			  	$user->OTC_DERIVATIVES_PERIOD = $step5_data['derivativesPeriod'];
			  	$user->KNOWLEDGE_QUALIFICATION = $step5_data['qualified'];
			  	$user->REFERER = $step5_data['referer'];
			  	
			  	
			  	if(($step1_data['accountType'] == "joint_survivorship" )|| ($step1_data['accountType'] == "joint_common" )){
		    	/**
			  	 * 
			  	 * STEP 6
			  	 */
			  	
				  	$jointUser = new JointUser();
				  	$step6_data = $this->session->userdata('step6_data');
				  	$jointUser->TITLE = $step6_data['jointTitle'];
				  	$jointUser->FIRST_NAME = $step6_data['jointFirstName'];
			        $jointUser->MIDDLE_NAME = $step6_data['jointMiddleName'];
			    	$jointUser->LAST_NAME = $step6_data['jointLastName'];
				    $jointUser->COUNTRY_CODE = $step6_data['jointCountryID'];
				    $jointUser->ADDRESS_MAIN = $step6_data['jointAddressLine1Main'];
				    $jointUser->ADDRESS_SECOND = $step6_data['jointAddressLine2Main'];
				    $jointUser->CITY = $step6_data['jointCityMain'];
				    $jointUser->STATE = $step6_data['jointRegionMain'];
				    $jointUser->POSTAL_CODE = $step6_data['jointPCodeMain'];
				    $jointUser->CURRENT_ADDR_YEARS = $step6_data['jointCurrentYears'];
				    $jointUser->HOME_PHONE = $step6_data['jointHomePhoneMain1'] . "-" . $step6_data['jointHomePhoneMain2'] . "-" . $step6_data['jointHomePhoneMain3'];
				    $jointUser->MOBILE_PHONE = $step6_data['jointMobilePhoneMain1']. "-" . $step6_data['jointMobilePhoneMain2'] . "-" . $step6_data['jointMobilePhoneMain3'];
			      	
				    $jointPrevAddr = new PrevAddress();
				    if($step6_data['jointCurrentYears']  != '4'){
					    $jointPrevAddr->COUNTRY_CODE =  $this->model->get_country($step6_data['jointCountryPrev']);
				      	$jointPrevAddr->ADDRESS_MAIN = $step6_data['jointAddressLine1Prev'];
				      	$jointPrevAddr->ADDRESS_SECOND = $step6_data['jointAddressLine2Prev'];
				      	$jointPrevAddr->CITY = $step6_data['jointCityPrev'];
				      	$jointPrevAddr->STATE = $step6_data['jointRegionPrev'];
				      	$jointPrevAddr->POSTAL_CODE = $step6_data['jointPCodePrev'];
				    }
				    
			      	$jointUser->SEPARATE_MAILING = $step6_data['jointSeparateMailing'];
			      	
			      	$jointMailingAddr = new MailingAddress();
			      	if ($step6_data['jointSeparateMailing']) {
					    $jointMailingAddr->COUNTRY_CODE =  $step6_data['jointCountryMailing'];
					    $jointMailingAddr->ADDRESS_MAIN = $step6_data['jointAddressLine1Mailing'];
				    	$jointMailingAddr->ADDRESS_SECOND = $step6_data['jointAddressLine2Mailing'];
				      	$jointMailingAddr->CITY = $step6_data['jointCityMailing'];
				      	$jointMailingAddr->STATE = $step6_data['jointRegionMailing'];
				     	$jointMailingAddr->POSTAL_CODE = $step6_data['jointPCodeMailing'];
			      	}
			      	
			      	
				  	/**
				  	 * 
				  	 * STEP 7
				  	 */
			     	
				  	$step7_data = $this->session->userdata('step7_data');
			     	$jointUser->BIRTHDATE = $step7_data['jointBirthDate'];
				  	$jointUser->CITIZENSHIP_COUNTRY = $step7_data['jointCitizenship'];
				  	$jointUser->ISSUING_COUNTRY = $step7_data['jointIssuingCountry'];
				  	$jointUser->PASS_NUMBER = strlen($step7_data['jointPassNumber']) > 0 ? $step7_data['jointPassNumber'] : ""; 
				  	$jointUser->EXPIRY_DATE = $step7_data['jointExpiryDate'];
			      	
				  	
				  	/**
				  	 * 
				  	 * STEP 8
				  	 */
				  	$step8_data = $this->session->userdata('step8_data');
			     	$jointUser->EMPLOYMENT_STATUS = $step8_data['jointEmploymentStatus'];
				  	$jointUser->INCOME_TYPE_ID = $step8_data['jointAnualIncome'];
				  	$jointUser->SAVINGS_TYPE_ID = $step8_data['jointSavingInvest'];
				  	
				  	$f_sources = explode(";", $step8_data['jointFoundSource']);
				  	$jointUser->FUND_SOURCE = "";
				  	foreach ($f_sources as $item){
					  	$jointUser->FUND_SOURCE .= ($this->model->get_found_source($item) . "/");
				  	}
				  	$jointUser->FUND_SOURCE = trim_slashes($jointUser->FUND_SOURCE);
				  	
				  	$jointUser->FUNDS_AV_TYPE = $step8_data['jointAvailableFtype'];
				  	$jointUser->PROPERTY_OWNER = $step8_data['jointPropertyOwner'];
				  	$jointUser->PROPERTY_VALUE = $step8_data['jointPropertyValue'];
		
				  	$jointEmployer = new Employer();
				  	
				  	if($step8_data['jointEmploymentStatus'] == 'Employment'){
					  	$jointEmployer->E_NAME = $step8_data['jointEmployerName'];
					  	$jointEmployer->COUNTRY_ID = $step8_data['jointEmployerCountry'];
					  	$jointEmployer->ADDRESS_MAIN = $step8_data['jointAddressEmpl1'];
					  	$jointEmployer->ADDRESS_SECOND = $step8_data['jointAddressEmpl2'];
					  	$jointEmployer->CITY = $step8_data['jointCityEmpl'];
					  	$jointEmployer->STATE = $step8_data['jointRegionEmpl'];
					  	$jointEmployer->POSTAL_CODE = $step8_data['jointPCodeEmpl'];
					  	$jointEmployer->BUSINESS_NATURE = $step8_data['jointBusinessNature'];
					  	$jointEmployer->E_POSITION = $step8_data['jointPositionEmpl'];
					  	$jointEmployer->OVERSEAS_REGULATOR = $step8_data['jointOverseasRegulator'];
				  	}
				  	
				  	
				  	/**
				  	 * 
				  	 * Step 9
				  	 */
				  	$step9_data = $this->session->userdata('step9_data');
				  	$jointUser->SHARES_BONS_FREQUENCY = $step9_data['jointSharesFreq'];
				  	$jointUser->SHARES_BONS_PERIOD = $step9_data['jointSharesPeriod'];
				  	$jointUser->DERIVATIVES_FREQUENCY = $step9_data['jointTradingFreq'];
				  	$jointUser->DERIVATIVES_PERIOD = $step9_data['jointTradingPeriod'];
				  	$jointUser->OTC_DERIVATIVES_FREQUENCY = $step9_data['jointDerivativesFreq'];
				  	$jointUser->OTC_DERIVATIVES_PERIOD = $step9_data['jointDerivativesPeriod'];
				  	$jointUser->KNOWLEDGE_QUALIFICATION = $step9_data['jointQualified'];
			    }
			  	
			    
			    $step2_data = $this->session->userdata('step2_data');
			    $step4_data = $this->session->userdata('step4_data');
			    $step6_data = $this->session->userdata('step6_data');
			    $step8_data = $this->session->userdata('step8_data');

			    if($step4_data['employmentStatus'] == 'Employed'){
			  		$user->EMPLOYER_ID = $this->model->saveEmployer($employer);
			  	}
		    
			  	
			  	$userID = $this->model->saveUser($user);
			  	
		    	if($step2_data['currentYears']  != '4'){
			  		 $prevAddr->USER_ID = $userID; 
			  		 $this->model->savePrevAddr($prevAddr);
			  	}
			  	
			  	if ($step2_data['separateMailing']) {
			  		 $mailing->USER_ID = $userID;
			  		 $this->model->saveMailingAddr($mailing);
			  	}
			  	
	  			if(($step1_data['accountType'] == "joint_survivorship" )|| ($step1_data['accountType'] == "joint_common" )){
				  	$jointUserID = "";
				  	if(($step1_data["accountType"] == "joint_survivorship") || ($step1_data["accountType"] == "joint_common") ){
					  	
				  		if($step8_data['jointEmploymentStatus'] == 'Employed'){
			  	 			$jointUser->EMPLOYER_ID = $this->model->saveEmployer($jointEmployer);
			  	 		}
				  		$jointUser->USER_ID = $userID; 
					  	$jointUserID = $this->model->saveJointUser($jointUser);
				  	}
				  	
			  	 	if($step6_data['jointCurrentYears']  != '4'){
				  		$jointPrevAddr->JOINT_USER_ID = $jointUserID;
				  		$this->model->savePrevAddr($jointPrevAddr);
			  	 	}
			  	 	
		   			if ($step6_data['jointSeparateMailing']) {
		   				$jointMailingAddr->JOINT_USER_ID = $jointUserID; 
		   				$this->model->saveMailingAddr($jointMailingAddr);
			  	 	}
		  	 		  	 	
		    	}
		    }
	    	 //if($step1_data['accountType'] == 'corporate'){
	    	 else{
			  	/**
			  	 * 
			  	 * STEP 11
			  	 */
		    	
		    	$step11_data = $this->session->userdata('step11_data');
		    	$business = new BusinessUser(); 
		    	$business->COMPANY_NAME = $step11_data['companyName'];
		    	
				$business->REG_NUMBER = $step11_data['regNumber'];
				$business->INCORP_COUNTRY = $step11_data['countryCorpID'];
			    $business->INCORP_DATE = $step11_data['incorpDate'];
				$business->REG_COUNTRY = $step11_data['countryRegID'];
			    $business->ADDRESS_MAIN = $step11_data['addressLine1Main'];
				$business->ADDRESS_SECOND = $step11_data['addressLine2Main'];
			    $business->CITY = $step11_data['cityMain'];
			    $business->STATE = $step11_data['regionMain'];
			    $business->POSTAL_CODE = $step11_data['pCodeMain'];
			    $business->PHONE = $step11_data['phone1'] . "-" . $step11_data['phone2'] . "-" . $step11_data['phone3'];
		    	$business->MOBILE_PHONE = $step11_data['mobilePhone1'] . "-" . $step11_data['mobilePhone2'] . "-" . $step11_data['mobilePhone3'];
			    
		    	$businessMailing = new MailingAddress();
		    	
				$business->SEPARATE_MAILING = $step11_data['separateMailing'];
				if($business->SEPARATE_MAILING){
					$businessMailing->COUNTRY_CODE = $step11_data['countryMailing'];
				    $businessMailing->ADDRESS_MAIN = $step11_data['addressLine1Mailing'];
				    $businessMailing->ADDRESS_SECOND = $step11_data['addressLine2Mailing'];
				    $businessMailing->CITY = $step11_data['cityMailing'];
				    $businessMailing->STATE = $step11_data['regionMailing'];
				    $businessMailing->POSTAL_CODE = $step11_data['pCodeMailing'];
				}
				
				$business->POSITION_PRINCIPAL = $step11_data['position'];
				$business->OFF1_NAME = $step11_data['offName1'];
				$business->OFF2_NAME = $step11_data['offName2'];
				$business->OFF3_NAME  = $step11_data['offName3'];
				$business->OFF1_POSITION = $step11_data['offPosition1'];
				$business->OFF2_POSITION = $step11_data['offPosition2'];
				$business->OFF3_POSITION = $step11_data['offPosition3'];
				
				
				/**
				 * STEP 12
				 */
				
				$step12_data = $this->session->userdata('step12_data');
				$business->BUSINESS_TYPE = $step12_data['businessType'];
				$business->REG_INVESTMENT = $step12_data['regInvestment'];
				$business->REGULATOR = $step12_data['regulator'];
			    $business->BREG_NUMBER = $step12_data['bRegNumber'];
				$business->ANUAL_REVENUE = $step12_data['anualRevenue'];
			    $business->NETWORTH = $step12_data['networth'];
			    
			    $business->BANK_NAME = $step12_data['bankName'];
			    $business->BANK_COUNTRY = $step12_data['bankCountry'];
			    $business->BANK_ADDRESS_MAIN = $step12_data['addressLine1Bank'];
			    $business->BANK_ADDRESS_SECOND = $step12_data['addressLine2Bank'];
			    $business->BANK_CITY = $step12_data['cityBank'];
				$business->BANK_STATE = $step12_data['regionBank'];
				$business->BANK_POSTAL_CODE = $step12_data['pCodeBank'];
			    $business->BANK_ACCOUNT_NAME = $step12_data['accName'];
			    $business->BANK_ACCOUNT_NUMBER = $step12_data['accNumber'];
			    $business->BANK_SORT_CODE = $step12_data['sortCode'];

				/**
				 * STEP 13
				 */	
			    			
				$step13_data = $this->session->userdata('step13_data');
				$user->INITIAL_DEPOSIT = $step13_data['initialDeposit'];
		  		$user->SHARES_BONS_FREQUENCY = $step13_data['sharesFreq'];
				$user->SHARES_BONS_PERIOD = $step13_data['sharesPeriod'];
				$user->DERIVATIVES_FREQUENCY = $step13_data['tradingFreq'];
				$user->DERIVATIVES_PERIOD = $step13_data['tradingPeriod'];
				$user->OTC_DERIVATIVES_FREQUENCY = $step13_data['derivativesFreq'];
				$user->OTC_DERIVATIVES_PERIOD = $step13_data['derivativesPeriod'];
				$user->KNOWLEDGE_QUALIFICATION = $step13_data['qualified'];
				$user->REFERER = $step13_data['referer'];
				

				$business->USER_ID = $this->model->saveUser($user); 
				$businessID = $this->model->saveBusiness($business);
				if(!$businessID){
					$data['error'] = "Error saving user:" . mysql_error();
				}
				else{
//					$data['success'] = "";
				}
				if($business->SEPARATE_MAILING){
					$businessMailing->BUSINESS_USER_ID = $businessID;
					$this->model->saveMailingAddr($businessMailing);
				}
				
	  	 
		    }
	  	 	
		  	$this->session->sess_destroy();
		  	
		  	$data['success'] = "Your account was created successfully";
		  	$data['refresh'] = true;
		  	$data['delay'] = 10;
		  	$data['link'] = site_url('user/create_account/step/final#message');
		  	
		  	echo json_encode($data);		    
		    break;
		 }
	  }
    }

      /*this function returns the country phone code for the specific country ID*/
      function code($c_id){
	    if(is_numeric($c_id)){
	      $data['code'] = $this->db->get_countries($c_id);
	    }
	    else{
	      $data['error'] = "Not a valid Country";
	    }

	    echo json_encode($data);
      }
}